/**
 * Created with IntelliJ IDEA
 * Description:
 * User: Administrator
 * Data: 2023 - 05 - 20
 * Time: 19:19
 */
//1、无头单向非循环链表实现
public class SingleLinkList {
    static class LinkNode {
        public int value;
        public LinkNode next;

        public LinkNode(int value) {
            this.value = value;
        }
    }
    public LinkNode head;
    public void creatSingleLinkList() {
        LinkNode node1 = new LinkNode(1);
        LinkNode node2 = new LinkNode(2);
        LinkNode node3 = new LinkNode(3);
        LinkNode node4 = new LinkNode(4);
        LinkNode node5 = new LinkNode(5);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        head = node1;
    }
    public void display() {
        LinkNode cur = this.head;
        while(cur != null) {
            System.out.print(cur.value + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    //得到单链表的长度
    public int size() {
        int count = 0;
        LinkNode cur = this.head;
        while(cur != null) {
            cur = cur.next;
            count++;
        }
        return count;
    }
    //头插法
    public void addFirst(int data) {
        LinkNode cur = new LinkNode(data);
        cur.next = this.head;//如果head为null这样写也可以
        this.head = cur;
    }
    //尾插法
    public void addLast(int data) {
        LinkNode cur1 = new LinkNode(data);
        LinkNode cur = this.head;
        if(cur == null) {//要另外考虑head为null的情况
            this.head = cur1;
            return;
        }
        while(cur.next != null) {//注意这里是cur.next!=null而不是cur!=null，因为要找到最后一个节点
            cur = cur.next;
        }
        cur.next = cur1;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        LinkNode cur = this.head;
        while(cur != null) {
            if(cur.value == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    private LinkNode addIndexSubOne(int index) {
        LinkNode cur = this.head;
        while((index - 1) != 0) {//找到要插入位置下标的前一个节点，因为要通过这个节点找到后面节点的引用
            cur = cur.next;
            index--;
        }
        return cur;
    }
    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data) {
        LinkNode cur1 = new LinkNode(data);
        if((index < 0) || (index > size())) {
            System.out.println(index + "坐标不合法");
            return false;
        }
        if(index == 0) {//在0下标处插入数据
            addFirst(data);
            return true;
        }
        LinkNode cur = addIndexSubOne(index);
        cur1.next = cur.next;//重点，对在size()下标处插入数据同样适用
        cur.next = cur1;//重点
        return true;
    }

    private LinkNode removeSubOne(int key) {
        LinkNode cur = this.head;
        while(cur.next != null) {//找到要删除数的前一个节点，因为要通过这个节点找到后面节点的引用
            if(cur.next.value == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if(head == null) {
            return;
        }
        if(this.head.value == key) {//删除第一个节点
            this.head = this.head.next;
            return;
        }
        LinkNode cur = removeSubOne(key);
        if(cur == null) {
            System.out.println("要删除的关键字不存在");
            return;
        }
        LinkNode del = cur.next;//重点，对删除最后一个节点同样适用
        cur.next = del.next;//重点
    }
    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if(this.head == null) {
            return;
        }
        LinkNode cur = head.next;//cur为要删除的节点
        LinkNode pre = head;//pre为要删除节点的前驱
        while(cur != null) {
            if(cur.value == key) {//对删除最后一个节点同样适用
                pre.next = cur.next;
                cur = cur.next;
            }else {
                pre = cur;
                cur = cur.next;
            }
        }
        if(this.head.value == key) {
            this.head = this.head.next;
        }
    }
    public void clear() {
         this.head = null;
    }
}

